<template>
	<div class="teacherHomework w944">
		<alert ref="alert"></alert>
		<deleteAlert ref="deleteAlert"></deleteAlert>
		<courseWareData ref="courseWareData"></courseWareData>
		<div class="label_top">
			<button @click="reBack"><i><&nbsp;</i>返回</button>
			<p>{{updateTitle}}</p>
		</div>
		<div class="label_table">
			<div class="el-tabs-report">
				<div class="msearch">
					<input placeholder="作业名称、所属课程等关键词……" v-model="search"/>
					<a @click="getTableList"><img src="../../assets/qudaoImg/search.png"/></a>
				</div>
			</div>
			<el-table ref="multipleTable" :data="tableData" border @selection-change="handleSelectionChange">
				<el-table-column label="序号" width="55">
					<template slot-scope="scope">{{ scope.row.sort}}</template>
				</el-table-column>
				<el-table-column prop="name" label="作业名称"></el-table-column>
				<el-table-column prop="homeworkTypeAlias" label="属性" width="70"></el-table-column>
				<el-table-column prop="courseName" label="所属课程"></el-table-column>
				<el-table-column label="作业管理" width="80">
					<template slot-scope="scope">
						<el-button @click="handleStatus(scope.row)">查看</el-button>
					</template>
				</el-table-column>
				<el-table-column label="数据管理" width="80">
					<template slot-scope="scope">
						<el-button @click="liveData(scope.row)">查看</el-button>
					</template>
				</el-table-column>
				<el-table-column label="评论" width="80">
					<template slot-scope="scope">
						<el-button @click="handleComment(scope.row)">查看</el-button>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="110">
					<template slot-scope="scope">
						<el-button @click="handleEdit(scope.row)">编辑</el-button>
						<el-button @click="handleDelete(scope.row)" class="red">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<div class="fenyeMargin">
				<div class="pag_block">
					<el-pagination @current-change="handleCurrentChange" :current-page="currentpage" :page-size="pagesize" layout="prev, pager, next" :total="total"></el-pagination>
				</div>
			</div>
		</div>
	</div>
</template>
<script>
	import axios from 'axios';
	import echarts from 'echarts';
	import alert from '../../components/Alert.vue';
	import deleteAlert from '../../components/deleteAlert.vue';
	import courseWareData from '../../components/courseWareData.vue';
	export default {
		data() {
			return {
				updateTitle:'',
				speakerId:'', //从讲师页面带过来的id
				sort:'',
				teacher:'',
				search: "",
				tableData: [],
				pagesize: 15,
				currentpage: 1,
				total: 1,
				multipleSelection: [],
				deleteType:'delete',
//				课件小节学习数据
				rowId:'',
				rowName:'',
				typeName:'',
				homeworkType:'',
				coursewareType:''
			}
		},
		methods: {
			getParams() {
				this.updateTitle = this.$route.params.updateTitle;
				this.speakerId = this.$route.params.speakerId;
				console.log('teacherHomework:',this.$route.params);
			},
			getTableList(){
				this.$ajax({
					url: this.$root.jifuUrl + "/mns/coursewareHomework/coursewareHomeworkSpeakerList",
					headers:{'ContentType': 'application/json; charset=utf-8'},
					method: 'POST',
					data: {
						page:this.currentpage,
						num:this.pagesize,
						rootcompanyid:1,
						speakerId:this.speakerId
					},
					success: res => {
						if(res.data.code == 200) {
							this.tableData = res.data.data.list;
							this.total=res.data.data.pageParam.items;
							for(var i = 0;i<this.tableData.length;i++){
								this.tableData[i].sort=i+1
							}
						}
					}
				});
			},
			handleStatus(row){
				this.$router.push({
					name: 'teacherhomeworkStatus',
					params: {
						courseid:row.id,
						coursewareId: row.courseid,
						courseName:row.name,
						homeworkType:row.homeworkType,
						coursewareType:row.coursewareType,
						teacherTitle:this.updateTitle,
						speakerId:this.speakerId,
						coursewareId:row.id
					}
				})
			},
			liveData(row){
				this.rowId = row.id;
            	this.rowName = row.name;
            	this.isBm = row.isBm;
            	this.typeName = row.coursewareTypeAlias;
            	this.homeworkType = row.homeworkType;
            	this.coursewareType = row.coursewareType;
				this.$refs.courseWareData.liveDataWrap = true;
				this.$refs.courseWareData.courseData();
			},
			toggleSelection(rows) {
				if(rows) {
					rows.forEach(row => {
						this.$refs.multipleTable.toggleRowSelection(row, !this.allSelect)
					})
					this.allSelect = !this.allSelect;
				}
			},
			handleSelectionChange(val) {
				this.multipleSelection = [];
				val.forEach(row => {
					this.multipleSelection.push(row.id);
				})
			},
			newLiveColumn(){
				this.$router.push({
					name: 'newLiveColumn',
					params: {
						liveId: this.liveId,
						liveType:1
					}
				})
			},
			handleDelete(row){
				this.transitionRow = row;
				this.$rootFunction().rootDelete();
			},
			handleDeleteAll() {
				this.$rootFunction().rootDeleteAll();
			},
			deleteSubmit() {
				if(this.deleteType == 'delete'){
					var s={ids:this.transitionRow.id};
					this.$ajax({
						url: this.$root.jifuUrl + "/deleteLectureById",
						method: 'POST',
						data: this.qs.stringify(s),
						success: res => {
							if(res.data.code == 200) {
								this.$rootFunction().deleteResSuccess();
							}else{
								this.failText = res.data.message;
								this.$rootFunction().deleteResFail();
							}
						}
					});
				}else{
					var s={ids:this.multipleSelection.join(",")};
					this.$ajax({
						url: this.$root.jifuUrl + "/deleteLectureById",
						method: 'POST',
						data: this.qs.stringify(s),
						success: res => {
							if(res.data.code == 200) {
								this.$rootFunction().deleteResSuccess();
							}else{
								this.failText = res.data.message;
								this.$rootFunction().deleteResFail();
							}
						}
					});
				}
			},
			handleCurrentChange(value) {
				this.currentpage = value;
				this.getTableList();
			},
			handleEdit(row) {
				this.$ajax({
					url: this.$root.jifuUrl + "/mns/coursewareHomework/coursewareHomeworkDetails",
					headers:{'ContentType': 'application/json; charset=utf-8'},
					method: 'POST',
					data: {id:row.id},
					success: res => {
						if(res.data.code == 200) {
							this.$router.push({
								name: 'edithomework',
								params: {
									type: 'edit',
									liveId:this.speakerId,
									updateTitleCourse:this.updateTitle
								}
							})
							this.$root.homeworkMsg = res.data.data;
						}
					}
				});
			},
			handleComment(row) {
				this.$router.push({
					name: 'coursewareComment',
					params: {
						courseid: row.id,
						coursewareType:row.coursewareType,
						courseName:row.courseName,
						teacherTitle:this.updateTitle,
						speakerId:this.speakerId,
						nowBack:3
					}
				})
			},
			importDaoChu(){},
			reBack(){
				this.$router.push({
					name:'teacher'
				});
			}
		},
		components:{
			alert,
			deleteAlert,
			courseWareData
		},
		mounted: function() {
			this.getParams();
			if(this.speakerId==null){
				this.$store.commit('GET_TEA_KCLIST');
				var s =this.$store.state.teaKcList;
				this.updateTitle=s.updateTitle;
				this.speakerId=s.speakerId
			}
			this.getTableList();
			this.teacher = this.$root.huasan.teacher;
			window.addEventListener("beforeunload",()=>{
				s={
					updateTitle:this.updateTitle,
					speakerId : this.speakerId
				}
				this.$store.commit('SET_TEA_KCLIST', s);
			});
		}
	}
</script>